#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/8/17 11:16
# ===========================================
#       题目名称： 7. 整数反转
#       题目地址： https://leetcode.cn/problems/reverse-integer/
#       题目描述： https://note.youdao.com/s/RRXNSXjR
# ===========================================

import math


class Solution:
    """
        实现思路：
            定义数组进行反转
        -231 <= x <= 231 - 1
    """

    def reverse(self, x):
        if -10 < x < 10:
            return x
        stack = list(str(x))
        symbol = ""  # 符号
        if x < 0:
            symbol = stack.pop(0)
        stack.reverse()
        while len(stack) > 0 and stack[0] == 0:
            stack.pop(0)
        stack.insert(0, symbol)
        x = int("".join(stack))
        MAX_VALUE = math.pow(2, 31) - 1
        MIN_VALUE = math.pow(-2, 31)
        if not MIN_VALUE <= x <= MAX_VALUE:
            return 0
        return x


if __name__ == '__main__':
    s = Solution()
    print("321 =>", s.reverse(x=123))
    print("-321 =>", s.reverse(x=-123))
    print("21 =>", s.reverse(x=120))
    print("0 =>", s.reverse(x=0))
    print("0 =>", s.reverse(x=1534236469))
